/*
 * Copyright 2012 NJUT  qixiaobo. All rights reserved.
 */
package com.fujitsu.nanjing.tieba.service;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fujitsu.nanjing.tieba.base.BaseService;
import com.fujitsu.nanjing.tieba.constrant.CommonParamter;
import com.fujitsu.nanjing.tieba.ibator.CoreAttach;
import com.fujitsu.nanjing.tieba.ibator.CoreAttachCriteria;
import com.fujitsu.nanjing.tieba.ibator.CoreCreditsLog;
import com.fujitsu.nanjing.tieba.model.Attach;

/**
 * AttachService.java
 * 
 * @author Qixiaobo-win8
 */
@Service
@Transactional
public class AttachService extends BaseService<CoreAttach, CoreAttachCriteria> {
	@Resource
	private Attach attach;
	@Resource
	private CreditsLogService creditsLogService;
	@Override
	public CoreAttach findById(String id) {
		return attach.selectByPrimaryKey(Integer.parseInt(id));

	}

	@Override
	public CoreAttach insert(CoreAttach t) {
		t.setCreatetime(new Date());
		t.setUpdatetime(new Date());
		attach.insertSelective(t);
		return t;
	}

	@Override
	public void update(CoreAttach t) {
		attach.updateByPrimaryKey(t);

	}

	@Override
	public void delete(CoreAttach t) {
		attach.deleteByPrimaryKey(t.getAttachid());

	}
	/**
	 * 增加积分和扣除积分，并且增加下载数量
	 * */
	public void incDownloads(String attachid) {
		incDownloads(findById(attachid));
	}
	/**
	 * 增加积分和扣除积分，并且增加下载数量
	 * */
	public void incDownloads(CoreAttach t) {
		if (t != null) {

			String userId = getUserDetails().getUsername();
			if (t.getUserid().equals(userId)) {
				// 本人下载无效果
				return;
			}
			t.setDownloads(t.getDownloads() + 1);
			if (t.getCredits() > 0) {
				CoreCreditsLog log = new CoreCreditsLog();
				Date now = new Date();
				log.setFromuser(userId);
				log.setUserid(t.getUserid());
				log.setAction("附件" + t.getAttachid());
				log.setCreatetime(now);
				log.setCredits((short) t.getCredits().intValue());
				creditsLogService.insert(log);
			}
			update(t);
		}

	}
	/**
	 * 根据topicId获取相关的attach
	 * */
	public List<CoreAttach> getAllAttachesByTopicId(int topicId) {
		CoreAttachCriteria example = new CoreAttachCriteria();
		example.setOrderByClause("localid asc");
		example.createCriteria().andTopicidEqualTo(topicId)
				.andReplyidEqualTo(CommonParamter.DEFAULT_REPLYID);
		return attach.selectByExample(example);
	}

	/**
	 * 根据replyId获取相关的attach
	 * */
	public List<CoreAttach> getAllAttachesByReplyId(int replyId) {
		CoreAttachCriteria example = new CoreAttachCriteria();
		example.setOrderByClause("localid asc");
		example.createCriteria().andReplyidEqualTo(replyId);
		return attach.selectByExample(example);
	}

	public int countAttachs() {
		CoreAttachCriteria example = new CoreAttachCriteria();
		example.createCriteria().andTopicidIsNotNull();
		return attach.countByExample(example);
	}

	public byte[] getContent(CoreAttach attach) throws IOException {
		if (attach == null) {
			throw new IOException("attach is null");
		}
		String path = CommonParamter.webRoot + CommonParamter.uploadPath
				+ File.separatorChar + attach.getLocalname();
		File file = new File(path);

		FileInputStream is = new FileInputStream(file);
		byte[] abyte0 = new byte[(int) file.length()];
		is.read(abyte0);
		return abyte0;
	}
	public void deleteByReplyId(int replyId) {
		for (CoreAttach coreAttach : getAllAttachesByReplyId(replyId)) {
			delete(coreAttach);
		};
	}
	public void deleteByTopicId(int topicId) {
		for (CoreAttach coreAttach : getAllAttachesByTopicId(topicId)) {
			delete(coreAttach);
		};
	}

}
